/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.Utilities
*        文件名:             BaseConfiguration.cs
*        创建系统时间:       2012/11/2 19:07:07
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.Utilities
{
    using System;
    using System.Globalization;

    public class BaseConfiguration
    {
        public const string ALLOW_USER_REGISTER = "AllowUserRegister";
        public bool AllowUserRegister;
        public const string COMMON_WEBSERVICE = "CommonWebService";
        public string CommonWebService;
        public const string COMPANY_NAME = "ShowLight";
        public const string CONFIGURATION_FROM = "ConfigurationFrom";
        public ConfigurationCategory ConfigurationFrom;
        public const string CURRENT_LANGUAGE = "CurrentLanguage";
        public const string CURRENT_LOGIN_TO = "LoginTo";
        public const string CURRENT_PASSWORD = "CurrentPassword";
        public const string CURRENT_USERNAME = "CurrentUsername";
        public string CurrentLanguage;
        public const string CUSTOMER_COMPANYNAME = "CustomerCompanyName";
        public string CustomerCompanyName;
        public string DataBase;
        public const string DATABASE = "DataBase";
        public const string DATAECP_TYPE = "DataBaseType";
        public ECP.Utilities.DataBaseType DataBaseType;
        public const string DBCONNECTION_STRING = "DbConnectionString";
        public string DbConnectionString;
        public const string DBHELPER_ASSMELY = "DbHelperAssmely";
        public const string DBHELPER_CLASSNAME = "DbHelperClass";
        public string DbHelperAssmely;
        public string DbHelperClass;
        public string ExceptionInfo;
        public const string FILE_WEBSERVICE = "FileWebService";
        public string FileWebService;
        private static BaseConfiguration instance;
        public const string LOAD_USER = "LoadUser";
        public bool LoadUser;
        private static object locker = new object();
        public const string LOGIN_ASSEMBLY = "LoginAssembly";
        public const string LOGIN_FORM = "LoginForm";
        public string LoginAssembly;
        public string LoginForm;
        public const string MAIN_FORM = "MainForm";
        public string MainForm;
        public const string MAX_POOL_SIZE = "MaxPoolSize";
        public int MaxPoolSize;
        public const string MIN_POOL_SIZE = "MinPoolSize";
        public int MinPoolSize;
        public string Password;
        public const string PASSWORD = "Password";
        public const string RECORD_LOG = "RecordLog";
        public bool RecordLog;
        public const string REGISTER_KEY = "RegisterKey";
        public string RegisterKey;
        public const string ROOT_MENU_ID = "RootMenuID";
        public string RootMenuID;
        public const string RUN_MODE = "RunMode";
        public ECP.Utilities.RunMode RunMode;
        public const string SERVER_NAME = "ServerName";
        public string ServerName;
        public const string SERVICE_FACTORY = "ServiceFactory";
        public const string SERVICE_PATH = "ServicePath";
        public string ServiceFactory;
        public string ServicePath;
        public const string SOFT_FULLNAME = "SoftFullName";
        public const string SOFT_NAME = "SoftName";
        public string SoftFullName;
        public string SoftName;
        public string StartupPath;
        public string TimeFormat;
        public const string TRUST_LINK = "TrustLink";
        public bool TrustLink;
        public const string USE_LICENSED_PERMISSION = "UseLicensedPermission";
        public const string USE_SCOPEADMIN = "UseScopeAdmin";
        public bool UseLicensedPermission;
        public const string USER_NAME = "Username";
        public string Username;
        public bool UseScopeAdmin;
        public string Version;
        public const string VERSION = "Version";
        public string WebService;
        public const string WEBSERVICE = "WebService";
        public const string WEBSERVICE_PASSWORD = "WebServicePassword";
        public const string WEBSERVICE_USERNAME = "WebServiceUsername";
        public string WebServicePassword;
        public string WebServiceUsername;
        public const string WORKREPORT_WEBSERVICE = "WorkReportWebService";
        public string WorkReportWebService;

        public BaseConfiguration()
        {
            this.CustomerCompanyName = string.Empty;
            this.ConfigurationFrom = ConfigurationCategory.UserConfig;
            this.SoftName = string.Empty;
            this.SoftFullName = string.Empty;
            this.RootMenuID = string.Empty;
            this.CurrentLanguage = "zh-CHS";
            this.Version = "1.0";
            this.CommonWebService = string.Empty;
            this.WebService = string.Empty;
            this.FileWebService = string.Empty;
            this.WorkReportWebService = string.Empty;
            this.WebServiceUsername = "ShowLight";
            this.WebServicePassword = "ShowLight";
            this.DataBaseType = ECP.Utilities.DataBaseType.Sqlserver;
            this.ServerName = string.Empty;
            this.DataBase = string.Empty;
            this.TrustLink = true;
            this.Username = string.Empty;
            this.Password = string.Empty;
            this.MaxPoolSize = 1;
            this.MinPoolSize = 1;
            this.RegisterKey = string.Empty;
            this.StartupPath = string.Empty;
            this.DbConnectionString = string.Empty;
            this.ServiceFactory = "ServiceFactory";
            this.ServicePath = "ECP.Service";
            this.DbHelperClass = "ECP.DataAccess.Utilities.SqlHelper";
            this.DbHelperAssmely = "ECP.DataAccess.Utilities";
            this.LoginAssembly = "ECP.UILayer.WinForm.Login";
            this.LoginForm = "FrmLogin";
            this.MainForm = "MainForm";
            this.LoadUser = true;
            this.AllowUserRegister = true;
            this.ExceptionInfo = "请您联系: 一心一意  15062423834 552152867@qq.com 对软件进行注册.";
            this.TimeFormat = "HH:mm:ss";
        }

        public BaseConfiguration(string softName)
        {
            this.CustomerCompanyName = string.Empty;
            this.ConfigurationFrom = ConfigurationCategory.UserConfig;
            this.SoftName = string.Empty;
            this.SoftFullName = string.Empty;
            this.RootMenuID = string.Empty;
            this.CurrentLanguage = "zh-CHS";
            this.Version = "1.0";
            this.CommonWebService = string.Empty;
            this.WebService = string.Empty;
            this.FileWebService = string.Empty;
            this.WorkReportWebService = string.Empty;
            this.WebServiceUsername = "ShowLight";
            this.WebServicePassword = "ShowLight";
            this.DataBaseType = ECP.Utilities.DataBaseType.Sqlserver;
            this.ServerName = string.Empty;
            this.DataBase = string.Empty;
            this.TrustLink = true;
            this.Username = string.Empty;
            this.Password = string.Empty;
            this.MaxPoolSize = 1;
            this.MinPoolSize = 1;
            this.RegisterKey = string.Empty;
            this.StartupPath = string.Empty;
            this.DbConnectionString = string.Empty;
            this.ServiceFactory = "ServiceFactory";
            this.ServicePath = "ECP.Service";
            this.DbHelperClass = "ECP.DataAccess.Utilities.SqlHelper";
            this.DbHelperAssmely = "ECP.DataAccess.Utilities";
            this.LoginAssembly = "ECP.UILayer.WinForm.Login";
            this.LoginForm = "FrmLogin";
            this.MainForm = "MainForm";
            this.LoadUser = true;
            this.AllowUserRegister = true;
            this.ExceptionInfo = "请您联系: 一心一意  15062423834 552152867@qq.com 对软件进行注册.";
            this.TimeFormat = "HH:mm:ss";
            this.SoftName = softName;
        }

        public ConfigurationCategory GetConfiguration(string configuration)
        {
            ConfigurationCategory category = ConfigurationCategory.Configuration;
            foreach (ConfigurationCategory category2 in Enum.GetValues(typeof(ConfigurationCategory)))
            {
                if (category2.ToString().Equals(configuration))
                {
                    return category2;
                }
            }
            return category;
        }

        public ECP.Utilities.DataBaseType GetDataBaseType(string dataBase)
        {
            ECP.Utilities.DataBaseType sqlserver = ECP.Utilities.DataBaseType.Sqlserver;
            foreach (ECP.Utilities.DataBaseType type2 in Enum.GetValues(typeof(ECP.Utilities.DataBaseType)))
            {
                if (type2.ToString().Equals(dataBase))
                {
                    return type2;
                }
            }
            return sqlserver;
        }

        public string GetOleDbConnection()
        {
            return this.GetOleDbConnection(this.DataBaseType, this.DataBase, this.Username, this.Password, this.ServerName, this.TrustLink);
        }

        public string GetOleDbConnection(ECP.Utilities.DataBaseType dataBaseType, string dataBase, string userName, string password, string workstation, bool trustLink)
        {
            string str = string.Empty;
            switch (dataBaseType)
            {
                case ECP.Utilities.DataBaseType.Oracle:
                    if (!trustLink)
                    {
                        str = string.Format(CultureInfo.CurrentCulture, "Provider=OraOLEDB.Oracle.1;Data Source={0};User Id={1};Password={2};", new object[] { dataBase, userName, password });
                    }
                    return str;

                case ECP.Utilities.DataBaseType.Sqlserver:
                    if (!trustLink)
                    {
                        return string.Format(CultureInfo.CurrentCulture, "Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Max Pool Size={4}; Min Pool Size={5}", new object[] { workstation, dataBase, userName, password, this.MaxPoolSize, this.MinPoolSize });
                    }
                    return string.Format(CultureInfo.CurrentCulture, "Provider=SQLOLEDB;Data Source={0};Initial Catalog={1};Integrated Security=SSPI;Max Pool Size={2}; Min Pool Size={3}", new object[] { workstation, dataBase, this.MaxPoolSize, this.MinPoolSize });

                case ECP.Utilities.DataBaseType.Access:
                    if (dataBase.IndexOf(":") < 0)
                    {
                        dataBase = this.StartupPath + dataBase;
                    }
                    return string.Format(CultureInfo.CurrentCulture, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"{0}\";User Id={1};jet OleDB:Database Password={2}", new object[] { dataBase, userName, password });
            }
            return str;
        }

        public ECP.Utilities.RunMode GetRunMode(string runMode)
        {
            ECP.Utilities.RunMode local = ECP.Utilities.RunMode.Local;
            foreach (ECP.Utilities.RunMode mode2 in Enum.GetValues(typeof(ECP.Utilities.RunMode)))
            {
                if (mode2.ToString().Equals(runMode))
                {
                    return mode2;
                }
            }
            return local;
        }

        public void GetSetting()
        {
            if (this.ConfigurationFrom == ConfigurationCategory.RegistryKey)
            {
                RegistryHelper.GetConfig(Instance);
            }
            if (this.ConfigurationFrom == ConfigurationCategory.Configuration)
            {
                ConfigurationHelper.GetConfig(Instance);
            }
            if (this.ConfigurationFrom == ConfigurationCategory.UserConfig)
            {
                ConfigHelper.GetConfig(Instance);
            }
        }

        public static BaseConfiguration Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BaseConfiguration();
                        }
                    }
                }
                return instance;
            }
        }

        public string MySqlConnection
        {
            get
            {
                if (!string.IsNullOrEmpty(this.DbConnectionString))
                {
                    return this.DbConnectionString;
                }
                return string.Format(CultureInfo.CurrentCulture, "server={0};uid={1};password={2};database={3};pooling=false", new object[] { this.ServerName, this.Username, this.Password, this.DataBase });
            }
        }

        public string OleDbConnection
        {
            get
            {
                if (!string.IsNullOrEmpty(this.DbConnectionString))
                {
                    return this.DbConnectionString;
                }
                return this.GetOleDbConnection();
            }
        }

        public string OracleConnection
        {
            get
            {
                if (!string.IsNullOrEmpty(this.DbConnectionString))
                {
                    return this.DbConnectionString;
                }
                return string.Format(CultureInfo.CurrentCulture, "Data Source={0};Persist Security Info=no;User ID={1};Password={2}", new object[] { this.ServerName, this.Username, this.Password });
            }
        }

        public string SqlConnection
        {
            get
            {
                if (!string.IsNullOrEmpty(this.DbConnectionString))
                {
                    return this.DbConnectionString;
                }
                if (this.TrustLink)
                {
                    return string.Format(CultureInfo.CurrentCulture, "Server={0};Database={1};Integrated Security=True", new object[] { this.ServerName, this.DataBase });
                }
                return string.Format(CultureInfo.CurrentCulture, "Server={0};Database={1};Uid={2};Pwd={3};", new object[] { this.ServerName, this.DataBase, this.Username, this.Password });
            }
        }
    }
}